package com.xinweisi.dataserver.module.datastatistics.behaviordaily;

import com.xinweisi.dataserver.common.checkDBName.CheckDBNameService;
import com.xinweisi.dataserver.common.domain.ResponseDTO;
import com.xinweisi.dataserver.module.datastatistics.behaviordaily.domain.dto.BehaviorDailyQueryDTO;
import com.xinweisi.dataserver.module.datastatistics.behaviordaily.domain.entity.*;
import com.xinweisi.dataserver.module.datastatistics.behaviordaily.domain.vo.BehaviorDailyDataVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * BehaviorDailyService
 *
 * @author lijie1
 * @version 1.0
 * @date 2020/7/27 0027 11:09
 */
@Slf4j
@Service
public class BehaviorDailyService {
    @Autowired
    private BehaviorDailyDao behaviorDailyDao;

    @Autowired
    private CheckDBNameService checkDBNameService;

    /**
     *  查询数据
     * @param queryDTO
     * @author lijie1
     * @date 2020/7/27 0027 11:59
     * @return com.xinweisi.dataserver.common.domain.ResponseDTO<com.xinweisi.dataserver.module.datastatistics.behaviordaily.domain.vo.BehaviorDailyDataVO>
     */
    public ResponseDTO<BehaviorDailyDataVO> getData(BehaviorDailyQueryDTO queryDTO) {
        BehaviorDailyDataVO resultEntity = new BehaviorDailyDataVO();
        long registeredUsersSumTotal = 0;
        long onLineUsersSumTotal = 0;
        long rechargeUsersSumTotal = 0;
        long withdrawalUsersSumTotal = 0;
        long signInUsersSumTotal = 0;
        long showTradeFlowUsersSumTotal = 0;
        long partakeGameUsersSumTotal = 0;
        long partakeActivityUsersSumTotal = 0;
        for (int i=0;i<queryDTO.getDBNameList().size();i++){
            queryDTO.setDBName(queryDTO.getDBNameList().get(i));
            int flag = checkDBNameService.checkDBName(queryDTO.getDBName());
            if(flag==0){
                continue;
            }
            RegisteredUsersEntity registeredUsersEntity = new RegisteredUsersEntity();
            registeredUsersEntity.setDBName(queryDTO.getDBName().substring(queryDTO.getDBName().indexOf("_")+1));
            List<Map<String, Long>> users= behaviorDailyDao.selectRegisteredUsersData(queryDTO);

            OnLineUsersEntity onLineUsersEntity = new OnLineUsersEntity();
            onLineUsersEntity.setDBName(queryDTO.getDBName().substring(queryDTO.getDBName().indexOf("_")+1));
            List<Map<String, Long>> onLineUsers= behaviorDailyDao.selectOnLineUsersData(queryDTO);

            RechargeUsersEntity rechargeUsersEntity = new RechargeUsersEntity();
            rechargeUsersEntity.setDBName(queryDTO.getDBName().substring(queryDTO.getDBName().indexOf("_")+1));
            List<Map<String, Long>> rechargeUsers= behaviorDailyDao.selectRechargeUsersData(queryDTO);

            WithdrawalUsersEntity withdrawalUsersEntity = new WithdrawalUsersEntity();
            withdrawalUsersEntity.setDBName(queryDTO.getDBName().substring(queryDTO.getDBName().indexOf("_")+1));
            List<Map<String, Long>> withdrawalUsers= behaviorDailyDao.selectWithdrawalUsersData(queryDTO);

            SignInUsersEntity signInUsersEntity = new SignInUsersEntity();
            signInUsersEntity.setDBName(queryDTO.getDBName().substring(queryDTO.getDBName().indexOf("_")+1));
            List<Map<String, Long>>  signInUsers= behaviorDailyDao.selectSignInUsersData(queryDTO);

            ShowTradeFlowUsersEntity showTradeFlowUsersEntity = new ShowTradeFlowUsersEntity();
            showTradeFlowUsersEntity.setDBName(queryDTO.getDBName().substring(queryDTO.getDBName().indexOf("_")+1));
            List<Map<String, Long>> showTradeFlowUsers= behaviorDailyDao.selectShowTradeFlowUsersData(queryDTO);

            PartakeGameUsersEntity partakeGameUsersEntity = new PartakeGameUsersEntity();
            partakeGameUsersEntity.setDBName(queryDTO.getDBName().substring(queryDTO.getDBName().indexOf("_")+1));
            List<Map<String, Long>> partakeGameUsers= behaviorDailyDao.selectPartakeGameUsersData(queryDTO);

            PartakeActivityUsersEntity partakeActivityUsersEntity = new  PartakeActivityUsersEntity();
            partakeActivityUsersEntity.setDBName(queryDTO.getDBName().substring(queryDTO.getDBName().indexOf("_")+1));
            List<Map<String, Long>> partakeActivityUsers= behaviorDailyDao.selectPartakeActivityUsersData(queryDTO);

            if(queryDTO.getDBNameList().size()==1){
                List<Long> registeredUsersCounts = new ArrayList<>();
                for (Map<String, Long> obj:users) {
                    registeredUsersCounts.add(obj.get("num"));
                }
                registeredUsersEntity.setHoursData(registeredUsersCounts);

                List<Long> onLineUsersCounts = new ArrayList<>();
                for (Map<String, Long> obj:onLineUsers) {
                    onLineUsersCounts.add(obj.get("num"));
                }
                onLineUsersEntity.setHoursData(onLineUsersCounts);

                List<Long> rechargeUsersCounts = new ArrayList<>();
                for (Map<String, Long> obj:rechargeUsers) {
                    rechargeUsersCounts.add(obj.get("num"));
                }
                rechargeUsersEntity.setHoursData(rechargeUsersCounts);

                List<Long> withdrawalUsersCounts = new ArrayList<>();
                for (Map<String, Long> obj:withdrawalUsers) {
                    withdrawalUsersCounts.add(obj.get("num"));
                }
                withdrawalUsersEntity.setHoursData(withdrawalUsersCounts);

                List<Long> signInUsersCounts = new ArrayList<>();
                for (Map<String, Long> obj:signInUsers) {
                    signInUsersCounts.add(obj.get("num"));
                }
                signInUsersEntity.setHoursData(signInUsersCounts);

                List<Long> showTradeFlowUsersCounts = new ArrayList<>();
                for (Map<String, Long> obj:showTradeFlowUsers) {
                    showTradeFlowUsersCounts.add(obj.get("num"));
                }
                showTradeFlowUsersEntity.setHoursData(showTradeFlowUsersCounts);

                List<Long> partakeGameUsersCounts = new ArrayList<>();
                for (Map<String, Long> obj:partakeGameUsers) {
                    partakeGameUsersCounts.add(obj.get("num"));
                }
                partakeGameUsersEntity.setHoursData(partakeGameUsersCounts);

                List<Long> partakeActivityUsersCounts = new ArrayList<>();
                for (Map<String, Long> obj:partakeActivityUsers) {
                    partakeActivityUsersCounts.add(obj.get("num"));
                }
                partakeActivityUsersEntity.setHoursData(partakeActivityUsersCounts);

            }
            long registeredUsersTotal = 0;
            for (Map<String, Long> obj:users) {
                registeredUsersTotal = registeredUsersTotal+obj.get("num");
                registeredUsersSumTotal = registeredUsersSumTotal+obj.get("num");
            }
            registeredUsersEntity.setTotal(registeredUsersTotal);

            long onLineUsersTotal = 0;
            for (Map<String, Long> obj:onLineUsers) {
                onLineUsersTotal = onLineUsersTotal+obj.get("num");
                onLineUsersSumTotal = onLineUsersSumTotal+obj.get("num");
            }
            onLineUsersEntity.setTotal(onLineUsersTotal);

            long rechargeUsersTotal = 0;
            for (Map<String, Long> obj:rechargeUsers) {
                rechargeUsersTotal = rechargeUsersTotal+obj.get("num");
                rechargeUsersSumTotal = rechargeUsersSumTotal+obj.get("num");
            }
            rechargeUsersEntity.setTotal(rechargeUsersTotal);

            long withdrawalUsersTotal = 0;
            for (Map<String, Long> obj:withdrawalUsers) {
                withdrawalUsersTotal = withdrawalUsersTotal+obj.get("num");
                withdrawalUsersSumTotal = withdrawalUsersSumTotal+obj.get("num");
            }
            withdrawalUsersEntity.setTotal(withdrawalUsersTotal);

            long signInUsersTotal = 0;
            for (Map<String, Long> obj:signInUsers) {
                signInUsersTotal = signInUsersTotal+obj.get("num");
                signInUsersSumTotal = signInUsersSumTotal+obj.get("num");
            }
            signInUsersEntity.setTotal(signInUsersTotal);

            long showTradeFlowUsersTotal = 0;
            for (Map<String, Long> obj:showTradeFlowUsers) {
                showTradeFlowUsersTotal = showTradeFlowUsersTotal+obj.get("num");
                showTradeFlowUsersSumTotal = showTradeFlowUsersSumTotal+obj.get("num");
            }
            showTradeFlowUsersEntity.setTotal(showTradeFlowUsersTotal);

            long partakeGameUsersTotal = 0;
            for (Map<String, Long> obj:partakeGameUsers) {
                partakeGameUsersTotal = partakeGameUsersTotal+obj.get("num");
                partakeGameUsersSumTotal = partakeGameUsersSumTotal+obj.get("num");
            }
            partakeGameUsersEntity.setTotal(partakeGameUsersTotal);

            long partakeActivityUsersTotal = 0;
            for (Map<String, Long> obj:partakeActivityUsers) {
                partakeActivityUsersTotal = partakeActivityUsersTotal+obj.get("num");
                partakeActivityUsersSumTotal = partakeActivityUsersSumTotal+obj.get("num");
            }
            partakeActivityUsersEntity.setTotal(partakeActivityUsersTotal);



            resultEntity.getRegisteredUsersList().add(registeredUsersEntity);
            resultEntity.getOnLineUsersList().add(onLineUsersEntity);
            resultEntity.getRechargeUsersList().add(rechargeUsersEntity);
            resultEntity.getWithdrawalUsersList().add(withdrawalUsersEntity);
            resultEntity.getSingInUsersList().add(signInUsersEntity);
            resultEntity.getPartakeGameUsersList().add(partakeGameUsersEntity);
            resultEntity.getPartakeActivityUsersList().add(partakeActivityUsersEntity);
            resultEntity.getShowTradeFlowUsersList().add(showTradeFlowUsersEntity);
        }
        resultEntity.setRegisteredUsersSumTotal(registeredUsersSumTotal);
        resultEntity.setOnLineUsersSumTotal(onLineUsersSumTotal);
        resultEntity.setRechargeUsersSumTotal(rechargeUsersSumTotal);
        resultEntity.setWithdrawalUsersSumTotal(withdrawalUsersSumTotal);
        resultEntity.setSignInUsersSumTotal(signInUsersSumTotal);
        resultEntity.setShowTradeFlowUsersSumTotal(showTradeFlowUsersSumTotal);
        resultEntity.setPartakeGameUsersSumTotal(partakeGameUsersSumTotal);
        resultEntity.setPartakeActivityUsersSumTotal(partakeActivityUsersSumTotal);

        return ResponseDTO.succData(resultEntity);
    }
}
